// subpages0/auctionpage/auctionIndex/auctionIndex.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    title: "小米机<text>1564564654</text>小米运动蓝牙耳机"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    if (!app.globalData.plumSession) {
      console.log("暂未获取到session");
      app.wechatSq(that);
    } else {
      that.resuestauctionIndex();
      that.requestAuctionlist();
    }
  },
  resuestauctionIndex: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_auction_index'
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            auctionInfo: res.data.data,
            title: res.data.data.template.title,
            tempInfo: res.data.data.template
          })
          if (res.data.data.template.title) {
            app.setNavtitle(res.data.data.template.title);
          } else {
            app.setNavtitle("拍卖首页");
          }
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestAuctionlist: function () {
    var that = this;
    var page = that.data.page;
    var data = {
      map: 'applet_auction_list',
      page: page
    };
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(page);
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.auctionList ? that.data.auctionList : [];
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            auctionList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.auctionList);
        } else {
          if (page <= 0) {
            that.setData({
              auctionList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      showLoading: true,
      noMoretip: false,
      page: 0
    })
    that.resuestauctionIndex();
    that.requestAuctionlist();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestAuctionlist();
    }
  },
  toauctionDetail: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages0/auctionpage/auctionDetail/auctionDetail?id=' + id,
    })
  },
  toMyauction:function(){
    wx.navigateTo({
      url: '/subpages0/auctionpage/myAuction/myAuction',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var title = that.data.tempInfo.title;
    app.getPoint(that);
    return {
      title: title,
      path: '/subpages0/auctionpage/auctionIndex/auctionIndex?enterfrom=share'
    }
  }
})